import 'package:flutter/material.dart';

class NewsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _NewsPageState();
  }
}

class _NewsPageState extends State<NewsPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('1. 页面创建时 initState ');
    //数据 控制器 依赖
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    //在 initState 方法之后执行
    print('2. 依赖变化  didChangeDependencies ');
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
    print('移除时调用 deactivate');
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    print('页面销毁时 dispose');
    //释放资源 取消订阅 清理控制器
  }

  @override
  void didUpdateWidget(covariant NewsPage oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('更新阶段 didUpdateWidget');
  }

  @override
  Widget build(BuildContext context) {
    print('3、页面刷新  build');
    return Scaffold(
      appBar: AppBar(title: Text('新闻页面')),
      body: SizedBox.expand(
        child: Container(
          color: Colors.purple[100],
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                OutlinedButton(
                  onPressed: () {
                    a = 4000;
                    setState(() {});
                  },
                  child: Text('刷新'),
                ),
                //自定义的View
                CustomView(count: a),
              ],
            ),
          ),
        ),
      ),
    );
  }

  int a = 2000;
}

class CustomView extends StatefulWidget {
  final int count;

  const CustomView({super.key, required this.count});

  @override
  State<StatefulWidget> createState() {
    return _CustomViewState();
  }
}

class _CustomViewState extends State<CustomView> {
  int _count = 1000;

  @override
  Widget build(BuildContext context) {
    print('子View 的 build');
    return Container(color: Colors.blue, child: Text('我是子View  $_count'));
  }

  @override
  void didUpdateWidget(covariant CustomView oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    print('子View 的 didUpdateWidget');
    _count = widget.count;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('子View 的initState');
    _count = widget.count;
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    print('子View 的 didChangeDependencies');
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
    print('子View 的 deactivate');
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    print('子View 的 dispose');
  }
}
